package com.zany.system.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.zany.system.service.RoleResourcesService;
import com.zany.system.vo.RoleResourcesVO;
import com.zany.system.vo.UserInfoVO;
import com.zany.util.ZanyErrorMsg;
import com.zany.util.ZanyException;
import com.zany.util.BeansUtil.RequestBeanUtil;
import com.zany.util.action.AbstractActionSupport;
import com.zany.util.ajax.AjaxTools;
import com.zany.util.json.JSONArray;
import com.zany.util.json.JSONException;
import com.zany.util.json.JSONObject;
import com.zany.util.json.JsonTools;


/**
 * 用户权限分配
 * @author cy
 * @version 1.0
 */
public class RoleResourcesAction extends AbstractActionSupport{


	private static final long serialVersionUID = 1445213384338464680L;
	private static Log logger = LogFactory.getLog(RoleResourcesAction.class);
	private RoleResourcesService roleResourcesService ;
	private List<RoleResourcesVO> roleResourcesList = new ArrayList<RoleResourcesVO>();
	private RoleResourcesVO roleResourcesVO = new RoleResourcesVO();
	private List<RoleResourcesVO> resourceList = new ArrayList<RoleResourcesVO>();
	private List<RoleResourcesVO> topCodeList = new ArrayList<RoleResourcesVO>();
	private List<UserInfoVO> roleList = new ArrayList<UserInfoVO>();
	
	public void getResourcesTop() throws ZanyException {
		if(logger.isInfoEnabled()){logger.info("RoleResourcesAction.getResourcesTop()");}
		response = ServletActionContext.getResponse();
		topCodeList = roleResourcesService.getTopCodeList();
		JSONArray listJson = new JSONArray();
		try{
			for(RoleResourcesVO vo:topCodeList){
				JSONObject json = new JSONObject();
				json.put("key",vo.getResourceCode());
				json.put("value",vo.getResourceName());
				listJson.put(json);
			}
		}catch(JSONException e){
			throw new ZanyException(ZanyErrorMsg.ERR_DEFAULT,e.getMessage(),e);
		}
		String json = "{ZANY:"+listJson.toString()+"}";
		AjaxTools.exAjax(json,response);
	}
	
	public void getResources() throws ZanyException{
		if(logger.isInfoEnabled()){logger.info("RoleResourcesAction.getResources()");}
		response = ServletActionContext.getResponse();
		roleResourcesVO.setTopCode(request.getParameter("topCode"));
		String start = request.getParameter("start");
		String pageStart = (start== null || start.equals("")?"1":start);
		Integer pageTotal = roleResourcesService.getResourcesCount(roleResourcesVO);
		roleResourcesVO.setPageMessage(Integer.valueOf(pageStart));
		resourceList = roleResourcesService.getResourcesList(roleResourcesVO);
		AjaxTools.exAjax("{results:" + pageTotal + ",ZANY:" + JsonTools.lstBeanToJson(resourceList) + "}", response);
	}
	
	public void getRole() throws ZanyException{
		if(logger.isInfoEnabled()){logger.info("RoleResourcesAction.getRole()");}
		response = ServletActionContext.getResponse();
		roleList = roleResourcesService.getRoleList();
		JSONArray listJson = new JSONArray();
		try{
			for(UserInfoVO vo:roleList){
				JSONObject json = new JSONObject();
				json.put("key",vo.getRoleCode());
				json.put("value",vo.getRoleName());
				listJson.put(json);
			}
		}catch(JSONException e){
			throw new ZanyException(ZanyErrorMsg.ERR_DEFAULT,e.getMessage(),e);
		}
		String json = "{ZANY:"+listJson.toString()+"}";
		AjaxTools.exAjax(json,response);
	}
	
	public void getRoleResources() throws ZanyException{
		if(logger.isInfoEnabled()){logger.info("RoleResourcesAction.getRoleResources()");}
		response = ServletActionContext.getResponse();
		roleResourcesVO.setRoleCode(request.getParameter("roleCode"));
		String start = request.getParameter("start");
		String pageStart = (start== null || start.equals("")?"1":start);
		roleResourcesVO.setPageMessage(Integer.valueOf(pageStart));
		Integer pageTotal = roleResourcesService.getRoleResourcesCount(roleResourcesVO);
		roleResourcesList = roleResourcesService.getRoleResourcesList(roleResourcesVO);
		AjaxTools.exAjax("{results:" + pageTotal + ",ZANY:" + JsonTools.lstBeanToJson(roleResourcesList) + "}", response);
	}
	
	public void insertRoleResource() throws Exception{
		if(logger.isInfoEnabled()){logger.info("RoleResourcesAction.insertRoleResource()");}
		response = ServletActionContext.getResponse();
		roleResourcesVO = (RoleResourcesVO)RequestBeanUtil.getReqToBean(RoleResourcesVO.class,request);
		RoleResourcesVO vo =  new RoleResourcesVO();
		vo = roleResourcesService.getRoleResources(roleResourcesVO);
		if(vo!=null){
			AjaxTools.exAjax("{\"success\":false,msg:\"该角色已拥有此权限！\"}",response);
		}else{
			vo = roleResourcesService.getTopRoleResources(roleResourcesVO);		
			try{
				if(vo==null){
					vo = roleResourcesService.getTopResources(roleResourcesVO);
					vo.setRoleCode(roleResourcesVO.getRoleCode());
					roleResourcesService.insertRoleResources(vo);
				}
					roleResourcesService.insertRoleResources(roleResourcesVO);
				AjaxTools.exAjax("{success:true}",response);
			}catch (Exception e) {
				AjaxTools.exAjax("{success:false}", response);
				throw new ZanyException(ZanyErrorMsg.ERR_DEFAULT,e.getMessage(),e);
			}
		}
	}
	
	public void delete() throws ZanyException{
		if(logger.isInfoEnabled()){logger.info("RoleResourcesAction.insertRoleResource()");}
		response = ServletActionContext.getResponse();
		String delId[] = request.getParameterValues("delId");
		roleResourcesVO.setRoleCode(request.getParameter("roleCode"));
		try{
			if(delId.length>0){
				for (int i = 0; i < delId.length; i++){
					roleResourcesVO.setResourceCode(delId[i]);
					roleResourcesService.dateleRoleResources(roleResourcesVO);
				}
				AjaxTools.exAjax("{success:true}", response);
			}
		}catch (Exception e) {
			AjaxTools.exAjax("{success:false}", response);
			throw new ZanyException(ZanyErrorMsg.ERR_DEFAULT,e.getMessage(),e);
		}
	}

	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
	}
	public RoleResourcesService getRoleResourcesService() {
		return roleResourcesService;
	}
	public void setRoleResourcesService(RoleResourcesService roleResourcesService) {
		this.roleResourcesService = roleResourcesService;
	}
}
